package spring.aop;

import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

import java.util.Arrays;

/**
 * @author 吴阳基
 * @datetime 2024/1/10 11:00
 */
@Aspect
public class UserServiceLogger {
    private static final Logger logger = Logger.getLogger(UserServiceLogger.class);

    @Before("execution(* spring.service.UserService.*(..))")
    public void before(JoinPoint jp) {
        logger.info("调用" + jp.getTarget() + "的 " + jp.getSignature().getName() + "方法。方法入参：" + Arrays.toString(jp.getArgs()));
    }

    @AfterReturning(pointcut = "execution(* spring.service.UserService.*(..))", returning = "result") // returning指定返回值的变量名，result为返回值变量名，可以自定义，但是必须
    public void afterReturning(JoinPoint jp, Object result) {
        logger.info("调用" + jp.getTarget() + "的 " + jp.getSignature().getName() + "方法。方法返回值：" + result);
    }

}
